package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.vnp.cooking.R;
import com.vnp.cooking.bean.ObjCachlam;
import com.vnp.cooking.utils.Utils;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewPagerStepCookpadAdapter extends PagerAdapter{
	
	Context context;
	/*String[] txtCountStep;
	String[] txtDecription;
	String[] urlStep;*/
	ArrayList<ObjCachlam> objCachlams ;
	ImageButton btnChupanhShare;
	LayoutInflater inflater;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
									.cacheInMemory(false)
									.cacheOnDisc(true)
									.displayer(new FadeInBitmapDisplayer(300))
									.build();
	
	/*public ViewPagerStepCookpadAdapter(Context context,String[] txtCountStep, String[] txtDecription, String[] urlStep) {
		this.context = context;
		this.txtCountStep = txtCountStep;
		this.txtDecription = txtDecription;
		this.urlStep = urlStep;
	}*/
	
	public ViewPagerStepCookpadAdapter(Context context, ArrayList<ObjCachlam> objCachlams) {
		this.context = context;
		this.objCachlams = objCachlams;
	}
	
	
	
	
	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((LinearLayout)object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		TextView txt_count_step;
		TextView txt_decription_step;
		ImageView img_step;
		
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.view_pager_step_cookpad_item, container, false);
		
		// khai bao cac thanh phan trong view pager item
		txt_count_step = (TextView) v.findViewById(R.id.txt_count_step);
		txt_decription_step = (TextView) v.findViewById(R.id.txt_decription_step);
		img_step = (ImageView) v.findViewById(R.id.img_step);
		/*// set gia tri cho cac phan tu
		txt_count_step.setText(txtCountStep[position]);
		txt_decription_step.setText(txtDecription[position]);
		ImageLoader.getInstance().displayImage(urlStep[position], img_step, options);
		*/
		// set font
		txt_count_step.setTypeface(Utils.getTypefaceRegular(context));
		txt_decription_step.setTypeface(Utils.getTypefaceRegular(context));
		
		txt_count_step.setText("Bước "+objCachlams.get(position).getStep());
		if (objCachlams.get(position).getDecriptionStep().toString().matches("")) {
			txt_decription_step.setText("");
		} else {
			txt_decription_step.setText(objCachlams.get(position).getDecriptionStep());
		}
		if (objCachlams.get(position).getUrlImageStep().toString().matches("")) {
			img_step.setVisibility(View.GONE);
			LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			txt_decription_step.setLayoutParams(Params1);
		} else {
			ImageLoader.getInstance().displayImage(objCachlams.get(position).getUrlImageStep(), img_step, options);
		}
		
		
		// kiem tra de hien thi nut chup anh share
		if (position == (getCount()-1)) {
			txt_count_step.setText("Hoàn thành");
		}
		
		
		// add viewpager item to viewpager
		((ViewPager) container).addView(v);
		
		
		return v;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((LinearLayout) object);
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
			return objCachlams.size();
	}


	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}
	
	

}
